// pages/plugins/activity/details/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    joinMemberPage: 1,
    // 分享标题
    shareAppMessageTitle: '',
    // 轮播显示的数量
    displayMultipleItems: 1,
    // 活动id
    id: '',
    info: {},
    contact: {
      // 报名状态
      sign_status: 0,
      // 是否收藏活动
      collect_status: 0,
      // 是否点赞活动
      fabulous_status: 0
    },

    // 弹窗
    showModal: false,
    concaterName: '',
    concaterMobile: '',

    joinMemberList: [],
    joinMemberListTotal: 0
  },

  /**
   * 控制显示
   */
  eject: function () {
    this.setData({
      showModal: true
    })
  },

  /**
   * 点击返回按钮隐藏
   */
  back: function () {
    this.setData({
      showModal: false
    })
  },

  /**
   * 获取input输入值
   */
  wish_name_put: function (e) {
    this.setData({
      concaterName: e.detail.value
    })
  },

  /**
   * 获取input输入值
   */
  wish_mobile_put: function (e) {
    this.setData({
      concaterMobile: e.detail.value
    })
  },

  /**
   * 点击确定按钮获取input值并且关闭弹窗
   */
  ok: function () {
    console.log(this.data.textV)
    if (this.data.concaterName === null || this.data.concaterName === ''){
      app.showConfirm('提示', '请输入联系人姓名', false);
      return;
    } else if (this.data.concaterMobile === null || this.data.concaterMobile === ''){
      app.showConfirm('提示', '请输入电话号码', false);
      return;
    }
    this.setData({
      showModal: false
    })
    var that = this;
    var params = {
      point_url: app.server.agentApi.joinActivity,
      activity_id: this.data.id,
      member_name: this.data.concaterName,
      mobile: this.data.concaterMobile
    }
    app.server.getConnect(params).then(function (res) {
      // wx.requestSubscribeMessage({
      //   tmplIds: ['TWxpJfPqOb_nhReYJMCCfiP4QTQu-HGyBDYPYSUrRHg'],
      //   success(res) {
      //     console.log(res)
      //   }
      // })
      wx.redirectTo({
        url: '/pages/agent/plugins/activity/result/index',
      })
    })
  },

  joinMemberCount: 1,
  bindJoinMemberListLoad() {
    this.joinMemberCount++;
    if(this.joinMemberCount===16){
      this.loadJoinMember();
    }
    if(this.joinActivity===20){
      this.joinMemberCount=1;
    }
  },

  loadJoinMember() {
    let that = this;
    var params = {
      point_url: app.server.agentApi.joinMemberList,
      pageNo: this.data.joinMemberPage++,
      pageSize: 20,
      id: that.data.id
    }
    app.server.getConnect(params).then(function (res) {
      if(res.data.total>1){
        that.setData({
          displayMultipleItems: 2
        })
      }
      that.setData({
        joinMemberList: that.data.joinMemberList.concat(res.data.rows),
        joinMemberListTotal: res.data.total
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
  },

  // 加载活动详情
  loadActivityDetails() {
    var that = this;
    var params = {
      point_url: app.server.agentApi.getActivityDetails,
      id: this.data.id
    }
    app.server.getConnect(params).then(function(res) {
      that.setData(res.data);
      that.setData({
        ['info.details']: res.data.info.details.replace(/\<img/gi, '<img style="max-width:100%;height:auto" '),
        // ['info.stopStatus']: !app.utils.compareDate(that.data.info.stop_time, new Date()),
        shareAppMessageTitle: res.data.info.name
      })
      // 加载评论
      that.getActivityCommentList();
      // 加载参与人员
      that.loadJoinMember();
      // that.setData({
      //   ['info.start_time']: app.utils.parseTime(res.data.info.start_time, '{y}年{m}月{d}日'),
      //   ['info.end_time']: app.utils.parseTime(res.data.info.end_time, '{y}年{m}月{d}日')
      // });
    })
  },

  // 加载活动评论列表
  getActivityCommentList() {
    var that = this;
    var params = {
      point_url: app.server.agentApi.getActivityCommentList,
      activityId: this.data.id
    }
    app.server.getConnect(params).then(function(res) {
      that.setData({
        commentList: res.data
      });
    })
  },

  // 拨打电话
  bindCall() {
    wx.makePhoneCall({
      phoneNumber: '15622283843'
    })
  },

  bindGetFormId(e){
    this.getFormId(e);
  },

  // 提交参加活动
  bindJoinActivity(e) {
    this.getFormId(e);
    this.eject();
  },

  // 分享活动
  bindShareActivity(e) {
    this.onShareAppMessage();
    this.getFormId(e);
  },

  // 收藏活动
  bindCollection(e) {
    this.getFormId(e);
    var that = this;
    var params = {
      point_url: this.data.contact.collect_status === 2 ? app.server.agentApi.cancelCollectionActivity : app.server.agentApi.doCollectionActivity,
      activityId: this.data.id
    }
    app.server.getConnect(params).then(function(res) {
      that.setData({
        ['contact.collect_status']: that.data.contact.collect_status === 1 ? 2 : 1
      })
    })
  },

  // 活动点赞
  bindLike(e) {
    this.getFormId(e);
    var that = this;
    var params = {
      point_url: this.data.contact.fabulous_status === 2 ? app.server.agentApi.cancelLikeActivity : app.server.agentApi.doLikeActivity,
      activityId: this.data.id
    }
    app.server.getConnect(params).then(function(res) {
      that.setData({
        ['contact.fabulous_status']: that.data.contact.fabulous_status === 1 ? 2 : 1
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 加载详情
    this.loadActivityDetails();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // 加载详情
    this.loadActivityDetails();
    // 加载评论
    this.getActivityCommentList();
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    // return {
    //   title: this.data.name,
    //   path: '/pages/agent/plugins/activity/details/index?id=' + this.data.id
    //   // imageUrl: app.config.url + this.data.picture,
    // }
  }
})
